home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / ptraceSun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  9.7 KB  |  339 lines

  1. /*
  2.  * ptrace.c --
  3.  *
  4.  *    Routines for creating a Unix like debugger interface to Sprite.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  * $Header: /sprite/src/kernel/mach/spur.md/RCS/machConfig.c,v 1.2 88/11/11 15:3
  16. 6:43 mendel Exp $ SPRITE (Berkeley)
  17.  */
  18.  
  19. #include <stdio.h> 
  20. #include "sprite.h"
  21. #include "status.h"
  22. #include <sys/types.h>
  23. #include "sys/ptrace.h"
  24. #include <errno.h>
  25. #include <proc.h>
  26. #include <signal.h>
  27. #include <sys/wait.h>
  28. #include "procDebugRegs.h"
  29.  
  30. static int mapStatusToErrno();
  31.  
  32. /* 
  33.  *----------------------------------------------------------------------
  34.  *
  35.  * ptrace --
  36.  *
  37.  *    Emulate Unix ptrace system call.
  38.  *
  39.  * Results:
  40.  *    An integer.
  41.  *
  42.  * Side effects:
  43.  *
  44.  *----------------------------------------------------------------------
  45.  */
  46.  
  47. int
  48. ptrace (request,pid,addr,data, addr2)
  49.      enum ptracereq request;    /* Data
  50.      int pid;            /* Process id of debugee. */
  51.      char *addr;        
  52.      int data;
  53.      char *addr2;
  54. {
  55.      extern int    errno;
  56.      Proc_DebugState process_state;
  57.      int    returnData;
  58.      ReturnStatus    status;
  59.  
  60.      errno = 0;
  61.      switch (request) {
  62.         case PTRACE_TRACEME: {      /* 0, by tracee to begin tracing */
  63.          extern int    _execDebug;
  64.          /*
  65.           * This is a hack to emulate the ptrace(0) request.  ptrace(0) 
  66.           * seems to cause the exec() to stop after the first instruction.
  67.           * Setting _execDebug to true causes the Proc_Exec call to
  68.           * enter the debug queue after the first instruction.
  69.           */
  70.           _execDebug = 1;
  71.           return 0;
  72.     }
  73.         case PTRACE_PEEKTEXT:    /* 1, read word from text segment */
  74.         case PTRACE_PEEKDATA:  { /* 2, read word from data segment */
  75.         status = Proc_Debug(pid,PROC_READ, sizeof(int),addr,&returnData);
  76.         errno = mapStatusToErrno(status);
  77.         return errno ? -1 : returnData; 
  78.         }
  79.         case PTRACE_POKETEXT:    /* 4, write word into text segment */
  80.         case PTRACE_POKEDATA: {  /* 5, write word into data segment */
  81.         status = Proc_Debug(pid,PROC_WRITE, sizeof(int),&data,addr);
  82.  
  83.         if (status != 0) {
  84.         fprintf(stderr,
  85.             "Proc_Debug failed status = %x, pid = %x, addr = %x\n",
  86.             status, pid, addr);
  87.         }
  88.  
  89.         errno = mapStatusToErrno(status);
  90.         return errno ? -1 : data; 
  91.         }
  92.         case PTRACE_CONT: {       /* 7, continue process */
  93.         if (data != 0 && data != 1) {
  94.         if (kill(pid,data) != 0) {
  95.             perror("kill");
  96.             return -1;
  97.         }
  98.         }
  99.         status = Proc_Debug(pid,PROC_CONTINUE,0,0,0);
  100.         errno = mapStatusToErrno(status);
  101.         return errno ? -1 : 0; 
  102.         }
  103.         case PTRACE_KILL: {      /* 8, terminate process */
  104.         if (kill(pid,SIGKILL) != 0) {
  105.         perror("kill");
  106.         return -1;
  107.         }
  108.         status= Proc_Debug(pid,PROC_DETACH_DEBUGGER,0,0,0);
  109.         (void) kill(pid,SIGKILL); /* Sometimes Sprite takes more than
  110.                        * one kill.
  111.                        */
  112.         (void) kill(pid,SIGCONT); 
  113.         status= Proc_Debug(pid,PROC_DETACH_DEBUGGER,0,0,0);
  114.         (void) kill(pid,SIGKILL); /* Sometimes Sprite takes more than
  115.                        * one kill.
  116.                        */
  117.         (void) kill(pid,SIGCONT); 
  118.         (void) kill(pid,SIGKILL); /* Sometimes Sprite takes more than
  119.                        * one kill.
  120.                        */
  121.         status= Proc_Debug(pid,PROC_DETACH_DEBUGGER,0,0,0);
  122.         if (status == PROC_INVALID_PID) {
  123.         status = SUCCESS;
  124.         }
  125.         errno = mapStatusToErrno(status);
  126.         return errno ? -1 : 0; 
  127.     }
  128.         case PTRACE_SINGLESTEP:{ /* 9, single step process */
  129.         status = Proc_Debug(pid,PROC_SINGLE_STEP,0,0,0);
  130.         errno = mapStatusToErrno(status);
  131.         return errno ? -1 : 0; 
  132.         }
  133.         case PTRACE_ATTACH: {  /* 10, attach to an existing process */
  134.         status = Proc_Debug(pid,PROC_GET_THIS_DEBUG,0,0,0);
  135.         errno = mapStatusToErrno(status);
  136.         return errno ? -1 : 0; 
  137.     }
  138.         case PTRACE_DETACH: {  /* 11, detach from a process */
  139.         if (data != 0 && data != 1) {
  140.         if (kill(pid,data) != 0) {
  141.             perror("kill");
  142.             return -1;
  143.         }
  144.         }
  145.         status = Proc_Debug(pid,PROC_DETACH_DEBUGGER,0,0,0);
  146.         errno = mapStatusToErrno(status);
  147.         return errno ? -1 : 0; 
  148.     }
  149.        case PTRACE_GETREGS: {     /* 12, get all registers */
  150.         status = Proc_Debug(pid,PROC_GET_DBG_STATE,0,0,&process_state);
  151.         errno = mapStatusToErrno(status);
  152.         procDebugToPtraceRegs(&process_state.regState, addr);
  153.         return errno ? -1 : 0; 
  154.     }
  155.        case PTRACE_SETREGS: {     /* 13, set all registers */
  156.         status = Proc_Debug(pid,PROC_GET_DBG_STATE,0,0,&process_state);
  157.         ptraceToProcDebugRegs(addr, &process_state.regState);
  158.         status = Proc_Debug(pid,PROC_SET_DBG_STATE,0,&process_state,0);
  159.         errno = mapStatusToErrno(status);
  160.         return errno ? -1 : 0; 
  161.     }
  162.  
  163.         case PTRACE_READTEXT:   /* 18, read text segment */
  164.         case PTRACE_READDATA: { /* 16, read data segment */
  165.         status = Proc_Debug(pid,PROC_READ, (int) data,addr,addr2);
  166.         errno = mapStatusToErrno(status);
  167.         return errno ? -1 : 0; 
  168.         }
  169.         case PTRACE_WRITEDATA:   /* 17, write data segment */
  170.         case PTRACE_WRITETEXT: {  /* 19 write data into data segment */
  171.         status = Proc_Debug(pid,PROC_WRITE, (int)data,addr2,addr);
  172.         errno = mapStatusToErrno(status);
  173.         return errno ? -1 : data; 
  174.         }
  175.        case PTRACE_GETFPREGS: {  /* 14, get all floating point regs */
  176.         return 0;
  177.        }
  178.        case PTRACE_SETFPREGS: { /* 15, set all floating point regs */
  179.         return 0;
  180.        }
  181.        case PTRACE_PEEKUSER:    /* 3, read word from user struct */
  182.        case PTRACE_POKEUSER:    /* 6, write word into user struct */
  183.        default: {
  184.         return -1;
  185.     }
  186.     }
  187. }
  188.  
  189. /* 
  190.  *----------------------------------------------------------------------
  191.  *
  192.  * wait --
  193.  *
  194.  *    Emulate the Unix wait system call when used to wait for a 
  195.  *    process being ptraced.
  196.  *
  197.  * Results:
  198.  *    An integer.
  199.  *
  200.  * Side effects:
  201.  *
  202.  *----------------------------------------------------------------------
  203.  */
  204. int
  205. wait (statusPtr)
  206.     union wait *statusPtr;
  207. {
  208.     ReturnStatus    status;
  209.     Proc_DebugState process_state;
  210.     extern int inferior_pid;
  211.  
  212.     if (!inferior_pid) {
  213.     error("Trying to wait for nonexistant process");
  214.     }
  215.     /*
  216.      * Wait for this process to enter the debug list.
  217.      */
  218.     status = Proc_Debug(inferior_pid,PROC_GET_THIS_DEBUG,0,0,0);
  219.     if (status == SUCCESS) {
  220.     if (statusPtr == (union wait *) 0) {
  221.         goto reapChild;
  222.         return inferior_pid;
  223.     }
  224.     status = Proc_Debug(inferior_pid,PROC_GET_DBG_STATE,0,0,&process_state);
  225.     if (status == SUCCESS) {
  226.         int unixSignal;
  227.             statusPtr->w_status = 0;
  228.             statusPtr->w_stopval = WSTOPPED;
  229.             if (process_state.termReason == PROC_TERM_SUSPENDED) {
  230.                 (void)Compat_SpriteSignalToUnix(process_state.termStatus,
  231.                            &unixSignal);
  232.                 statusPtr->w_stopsig = unixSignal;
  233.         if (process_state.termStatus == SIG_DEBUG ||
  234.             process_state.termStatus == SIG_TRACE_TRAP ||
  235.             process_state.termStatus == SIG_BREAKPOINT) {
  236.             statusPtr->w_stopsig = SIGTRAP;
  237.         }
  238.             } else if (process_state.termReason == PROC_TERM_SIGNALED ||
  239.                        process_state.termReason == PROC_TERM_RESUMED) {
  240.                 (void)Compat_SpriteSignalToUnix(process_state.termStatus, 
  241.                         &unixSignal);
  242.                 statusPtr->w_stopsig = unixSignal;
  243.         if (process_state.termStatus == SIG_DEBUG ||
  244.             process_state.termStatus == SIG_TRACE_TRAP ||
  245.             process_state.termStatus == SIG_BREAKPOINT) {
  246.             statusPtr->w_stopsig = SIGTRAP;
  247.         }
  248.             } else {
  249.         statusPtr->w_status = 0;
  250.                 statusPtr->w_retcode = process_state.termStatus;
  251.         (void) ptrace(PTRACE_DETACH,inferior_pid,0,SIGKILL,0);
  252.             }
  253.         return inferior_pid;
  254.     }
  255.     }
  256. reapChild:
  257.     { 
  258.         Proc_PID    pid;
  259.     int        reason, childStatus, subStatus;
  260.     status = Proc_Wait(1, (int *) &inferior_pid, PROC_WAIT_BLOCK, 
  261.         &pid, &reason,  &childStatus, &subStatus, 
  262.         (Proc_ResUsage *) NULL);
  263.         if (statusPtr != NULL)  {
  264.         int        unixSignal;
  265.             statusPtr->w_status = 0;
  266.             if (reason == PROC_TERM_SUSPENDED) {
  267.                 (void)Compat_SpriteSignalToUnix(childStatus, &unixSignal);
  268.                 statusPtr->w_stopval = WSTOPPED;
  269.                 statusPtr->w_stopsig = unixSignal;
  270.             } else if (reason == PROC_TERM_SIGNALED ||
  271.                        reason == PROC_TERM_RESUMED) {
  272.                 (void)Compat_SpriteSignalToUnix(childStatus, &unixSignal);
  273.                 statusPtr->w_stopval = WSTOPPED;
  274.                 statusPtr->w_termsig = unixSignal;
  275.             } else {
  276.                 statusPtr->w_retcode = childStatus;
  277.         (void) ptrace(PTRACE_DETACH,inferior_pid,0,SIGKILL,0);
  278.             }
  279.         }
  280.     }
  281.     return inferior_pid;
  282. }
  283.  
  284.  
  285.  
  286.  
  287. /* 
  288.  *----------------------------------------------------------------------
  289.  *
  290.  * mapStatusToErrno --
  291.  *
  292.  *    Map a Sprite status return code to a Unix errno. This is intended 
  293.  *    to work only on the status codes returns from Proc_Debug.
  294.  *
  295.  * Results:
  296.  *    An integer.
  297.  *
  298.  * Side effects:
  299.  *      None
  300.  *
  301.  *----------------------------------------------------------------------
  302.  */
  303.  
  304. static int
  305. mapStatusToErrno(status)
  306.     ReturnStatus    status;
  307. {
  308.     int    returnValue;
  309.  
  310.     if (status == SUCCESS) {
  311.     return 0;
  312.     }
  313.  
  314.     switch (status) {
  315.  
  316.     case PROC_INVALID_PID:
  317.     returnValue = ESRCH;
  318.     break;
  319.  
  320.     case SYS_INVALID_ARG:
  321.     returnValue = EINVAL;
  322.     break;
  323.  
  324.     case SYS_ARG_NOACCESS:
  325.     returnValue = EINVAL;
  326.     break;
  327.  
  328.     case GEN_ABORTED_BY_SIGNAL:
  329.     returnValue = EINTR;
  330.     break;
  331.  
  332.     default:
  333.     returnValue = EIO;
  334.     break;
  335.     }
  336.     return returnValue;
  337. }
  338.  
  339.